package com.taobao.action;

import java.util.ArrayList;
import java.util.List;

import com.taobao.pojo.Line;
import com.taobao.pojo.Smaller;
import com.taobao.pojo.Title;
import com.taobao.pojo.UserInfo;
import com.taobao.server.ConfigServer;
import com.taobao.ui.widget.SmallerWidget;
import com.taobao.ui.widget.UpdateSmallerWidget;

public class UpdateSmallerAction extends CommonAction {

    private ConfigServer configServer;

    @Override
    public void doAction(Object uiObject) {
        SmallerWidget smallerWidget = (SmallerWidget) uiObject;
        UserInfo userInfo = smallerWidget.getPojo(UserInfo.class);
        UpdateSmallerWidget updateSmallerWidget = smallerWidget.getUpdateSmallerWidget();
        Smaller smaller = updateSmallerWidget.getPojo(Smaller.class);
        List<Smaller> smallers = userInfo.getSmallers();

        List<Line> lines = new ArrayList<Line>();
        for (Smaller sm : smallers) {
            if (sm.getSmallerName().equals(smaller.getSmallerName())) {
                sm.setTbPwd(smaller.getTbPwd());
                sm.setTid(smaller.getTid());
            }
            lines.add(new Line(new Title(ConfigServer.SMALLERS), sm.getSmallerName(), sm.getPriority() + "," + sm.getTbPwd() + "," + sm.getTid()));
        }
        configServer.updateAll(ConfigServer.SMALLERS, lines);
        smallerWidget.fillWidget(userInfo);
    }

}
